/*
    Redmi K40
    srcirpt explain:auto connect wifi when disconnected
*/
let core = 1;
// 防止屏幕进入睡眠状态
device.keepScreenOn()
if(!requestScreenCapture()){
    toast("请求截图失败");
    exit();
}
sleep(1000);
home()
sleep(1000);
var settings = getPackageName("设置")
launch(settings)
sleep(1000)
click(497,1450)
sleep(5000)

//进入Wifi设置页面后
while(true){
    log("开始截图");
    var img = captureScreen();
    // images.saveImage(img, "/sdcard/1.png");
    var dot = img.pixel(664,912)//dot:-15891201
    img.recycle()
    if(dot!=-15891201){
        log("wifi断开");
        let refresh = images.read("/sdcard/wifiauto/refresh.png")
        while(true){
            let armMeAll = captureScreen()
            let res = findImage(armMeAll,refresh,{
                region:[840,805,1067-840,2268-805],
                threshold:0.95
            });
            armMeAll.recycle()
            if(res){
                log("has refresh")
                log(res.x)
                log(res.y)
                click(res.x+50,res.y+50)
                sleep(10000)

                let wifi = images.read("/sdcard/wifiauto/wifi"+core+".png")
                armMeAll = captureScreen()
                res = findImage(armMeAll,wifi,{
                    // region:[840,805,1067-840,2268-805],
                    threshold:0.95
                });
                armMeAll.recycle()
                if(res){
                    log("has wifi")
                    log(res.x)
                    log(res.y)
                    click(res.x+50,res.y+50)
                    sleep(10000)
                    break;
                }else{
                    log("wu wifi")
                };
            }else{
                log("wu refresh")
            };
        }
    }
    else{
        log("wifi已连接");
    }
    sleep(5000)
}
